
// 这就是那个用于改写用户问题的 chain

import { config } from "dotenv";
import { useModel } from "../../model";
import { ChatPromptTemplate, MessagesPlaceholder } from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { AIMessage, HumanMessage } from "@langchain/core/messages";


export const useRephraseChain = async () => {
    await config();
    const model = useModel({temperature: 0.2});
    const prompt = ChatPromptTemplate.fromMessages([
        [
            "system",
            "给定以下对话和一个后续问题，请将后续问题重述为一个独立的问题。请注意，重述的问题应该包含足够的信息，使得没有看过对话历史的人也能理解。",
          ],
          new MessagesPlaceholder("history"),
          ["human", "将以下问题重述为一个独立的问题：\n{question}"],
    ]);
    const rephraseChain = RunnableSequence.from([
        prompt,
        model,
        new StringOutputParser(),
    ]);
    return rephraseChain;
}

async function test() {
    const rephraseChain = await useRephraseChain();
    const historyMsgs = [new HumanMessage("hi, my name is '何凯明'"), new AIMessage("Hello Kai! It's great to meet you. I'm an AI assistant here to help with any questions or topics you'd like to discuss. Is there anything in particular on your mind today? Or would you like to just chat and get to know each other a bit? I'm happy to talk about pretty much any subject - from current events, to hobbies, to science and technology, to pop culture. The floor is yours!")];
    const question = "你觉得我的名字取的如何，有没有水平？";
    const res = await rephraseChain.invoke({
        history: historyMsgs,
        question: question,
    });
    console.log(res);
}

// test();
// 打印结果：
// 你觉得“何凯明”这个名字取得怎么样，有没有特别的含义或水平？